package com.ericsson.enap.modules.backup.task;

import com.ericsson.common.domain.CollectBizTypeEnum;
import com.ericsson.common.domain.NetTypeEnum;
import com.ericsson.common.quartz.task.ManualTask;
import com.ericsson.common.util.DateUtils;
import com.ericsson.common.util.FileUtil;
import com.ericsson.common.util.LogFileName;
import com.ericsson.common.util.LoggerUtils;
import com.ericsson.enap.modules.backup.constant.BackupConstant;
import com.ericsson.enap.modules.backup.service.VgcDataBackupService;
import com.ericsson.enap.modules.sys.entity.EnipNetelement;
import com.ericsson.enap.modules.sys.entity.EpcDict;
import com.ericsson.enap.modules.sys.service.EpcDictService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: Vgc 网元备份主任务入口
 * @author: wangjingshi
 * @create: 2021-09-02 15:25
 */
@Component
public class VgcDataBackupTask implements ManualTask {

    private Logger log = LoggerUtils.Logger(LogFileName.VgcDataBackupJob);

    @Autowired
    private VgcDataBackupService vgcDataBackupService;
    @Autowired
    private EpcDictService epcDictService;
    @Value("${collectorIp}")
    private String collectorIp;

    @Override
    public void execute() {
        long startTime = new Date().getTime();
        log.info(" ==== startTime :"+startTime);
        String date = DateUtils.formatDate(new Date());//yyyy-MM-dd
        int bizType = Integer.parseInt(CollectBizTypeEnum.VGCNetAutoBakup.value());//14
        List<EnipNetelement> list = vgcDataBackupService.queryNetelementList(bizType,collectorIp);
        Long amfType = Long.valueOf(NetTypeEnum.AMF.value());
        Long smfType = Long.valueOf(NetTypeEnum.SMF.value());
        Long upfType = Long.valueOf(NetTypeEnum.UPF.value());
        Long pcfType = Long.valueOf(NetTypeEnum.PCF.value());
        Long cgType = Long.valueOf(NetTypeEnum.CHF.value());
        List<EnipNetelement> amfList = list.stream().filter(x->x.getType()== amfType).collect(Collectors.toList());
        log.info(" ==== do AMF start ====");
        for (EnipNetelement amf:amfList){
            vgcDataBackupService.execAmfBackup(amf,date,log);
            vgcDataBackupService.execAmfLog(amf,date,log);
            cleanOldData(date,amf);
        }
        log.info(" ==== do AMF end ====");
        List<EnipNetelement> smfList = list.stream().filter(x->x.getType()== smfType).collect(Collectors.toList());
        log.info(" ==== do SMF start ====");
        for (EnipNetelement smf:smfList){
            vgcDataBackupService.execSmfBackup(smf,date,log);
            vgcDataBackupService.execSmfLog(smf,date,log);
            cleanOldData(date,smf);
        }
        log.info(" ==== do SMF end ====");
        List<EnipNetelement> upfList = list.stream().filter(x->x.getType()== upfType).collect(Collectors.toList());
        log.info(" ==== do UPF start ====");
        for (EnipNetelement upf:upfList){
            vgcDataBackupService.execSmfBackup(upf,date,log);
            vgcDataBackupService.execSmfLog(upf,date,log);
            cleanOldData(date,upf);
        }
        log.info(" ==== do UPF end ====");
        List<EnipNetelement> pcfList = list.stream().filter(x->x.getType()== pcfType).collect(Collectors.toList());
        log.info(" ==== do PCF start ====");
        for (EnipNetelement pcf:pcfList){
            vgcDataBackupService.execPcfBackup(pcf,date,log);
            vgcDataBackupService.execPcfLog(pcf,date,log);
            cleanOldData(date,pcf);
        }
        log.info(" ==== do PCF end ====");
        List<EnipNetelement> cgList = list.stream().filter(x->x.getType()== cgType).collect(Collectors.toList());
        log.info(" ==== do cg start ====");
        for (EnipNetelement cg:cgList){
            vgcDataBackupService.execCgBackup(cg,date,log);
            vgcDataBackupService.execCgLog(cg,date,log);
            cleanOldData(date,cg);
        }
        log.info(" ==== do cg end ====");
        long endTime = new Date().getTime();
        log.info(" ==== total time :"+(endTime-startTime)/1000/(double)60.00);
    }

    private void cleanOldData(String date, EnipNetelement vgcNet) {
        String cleanDays = epcDictService.selectEpcDict(new EpcDict("vgc_clean_day")).getKeyValue();
        String cleanDate = DateUtils.beforNumDay(new Date(),-Integer.parseInt(cleanDays));
        String cleanDir = BackupConstant.localPath + cleanDate + "/" +
                vgcNet.getType() + "/" + vgcNet.getName();
        File cleanFile = new File(cleanDir);
        FileUtil.deleteDir(cleanFile);
        vgcDataBackupService.deleteOldData(cleanDate);
    }

}
